// components/shareImg/shareImg.js
var QRCode = require('../../utils/weapp-qrcode.js')
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    zhuan_url:{
      type:String,
      value:'',
    },
    show_tghb: {
      type:Boolean,
      observer:'zhuanChange'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },
  ready(){
    var that = this;
    this.screenHeight = app.globalData.screenHeight;
    this.screenWidth = app.globalData.screenWidth;
    this.cur_product = app.globalData.cur_product
    this.user_info = app.globalData.userInfo;
    this.setData({
      code_w:this.calculate(156),
    })
    this.qrcode = new QRCode('canvas', {
      usingIn: this,
      text: "",
      image: '',
      width: this.calculate(156),
      height: this.calculate(156),
      colorDark: "#000000",
      colorLight: "white",
      correctLevel: QRCode.CorrectLevel.H,
    });
    this.ctx = wx.createCanvasContext('shareCanvas',that)
  },
  /**
   * 组件的方法列表
   */
  methods: {
    calculate(value){
      var _value = this.screenWidth / 750 * value
      return _value
    },
    changeHttp (value){
      var _value = value
      if(_value.indexOf("https")>-1 || _value.indexOf("http")>-1){
        if(_value.indexOf("https")==-1){
          _value = _value.replace('http','https')
        }
      }
      return _value
    },
    zhuanChange(e){
      var that = this;
      this.qrcode.makeCode(e,function(path){
        wx.canvasToTempFilePath({
          canvasId: 'canvas',
          success: (res1) => {
            that.triggerEvent('showImg',{qrcode_img:res1.tempFilePath})
            // wx.getImageInfo({
            //   src: that.changeHttp(that.cur_product.pict_main_url),//
            //   success (res) {
            //     that.ctx.drawImage('../../images/index/share_img.png', 0, 0, that.screenWidth, that.calculate(1305))
            //     that.ctx.drawImage(res.path, 0, 0, that.screenWidth, that.screenWidth)
            //     that.ctx.setTextAlign('left')
            //     that.ctx.setFillStyle('#0A0819')
            //     that.ctx.setFontSize(that.calculate(34))
            //     var temp = ''
            //     var t = that.cur_product.title
            //     var chr = t.split("");
            //     var row = [];
            //     var w = that.calculate(680)
            //     for (var a = 0; a < chr.length; a++) {
            //       if (that.ctx.measureText(temp).width < w) {
            //         temp += chr[a];
            //       } else {
            //           row.push(temp);
            //           temp = "";
            //       }
            //     }
            //     row.push(temp);
            //     for (var b = 0; b < row.length; b++) {
            //       if(b<2){
            //         that.ctx.fillText(row[b], that.calculate(30), that.calculate(768) + (b + 1) * that.calculate(54));
            //       }
            //     }

            //     that.ctx.setFillStyle('#F95A4B')
            //     that.ctx.setFontSize(that.calculate(60))
            //     var sale_prive = (that.cur_product.zk_price-that.cur_product.coupon_price).toFixed(2);
            //     var sale_prive_width = that.ctx.measureText(sale_prive.split('.')[0]).width
            //     that.ctx.fillText(sale_prive.split('.')[0], that.calculate(169), that.calculate(950));

            //     that.ctx.setFontSize(that.calculate(34))
            //     that.ctx.fillText(('.' + sale_prive.split('.')[1]), (that.calculate(169) + sale_prive_width), that.calculate(950));

            //     that.ctx.setFillStyle('#FFFFFF')
            //     that.ctx.setFontSize(that.calculate(50))
            //     that.ctx.setTextAlign('center')
            //     that.ctx.fillText(that.cur_product.coupon_price, that.calculate(486), that.calculate(972));

            //     that.ctx.setFillStyle('#9D9CA3')
            //     that.ctx.setFontSize(that.calculate(28))
            //     that.ctx.setTextAlign('left')
            //     that.ctx.fillText(that.cur_product.zk_price, that.calculate(160), that.calculate(1018));

            //     that.ctx.font=`bold 50px`
            //     that.ctx.setFillStyle('#0A0819')
            //     that.ctx.setFontSize(that.calculate(34))
            //     that.ctx.setTextAlign('left')
            //     that.ctx.fillText(that.user_info.name, that.calculate(220), that.calculate(1201));
            //     // ctx.save()
            //     wx.getImageInfo({
            //       src: that.user_info.headimgurl,//
            //       success (res) {
            //         console.log(res)
            //         that.ctx.drawImage(res1.tempFilePath, that.calculate(564), that.calculate(1072),that.calculate(156), that.calculate(156))
            //         that.ctx.arc(that.calculate(84), that.calculate(1204), that.calculate(54), 0, 2*Math.PI)
            //         that.ctx.clip()
            //         that.ctx.drawImage(res.path, that.calculate(0), that.calculate(0),res.width,res.height,that.calculate(30), that.calculate(1150),that.calculate(108), that.calculate(108))
            //         that.ctx.draw(false,()=>{
            //           wx.canvasToTempFilePath({
            //             canvasId: 'shareCanvas',
            //             success: (res2) => {
            //               that.triggerEvent('showImg',{url:res2.tempFilePath,qrcode_img:res1.tempFilePath})
            //             }
            //           },that)
            //         })
            //       }
            //     })
            //   }
            // })
          }
        },that)
      })
    },
  }
})
